package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryL1VO;
import com.cskaoyan.bean.CategoryListVO;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    private List<CategoryListVO> categoryListVO;

    @Override
    public List<CategoryListVO> allCategoryVO() {
        categoryListVO = categoryMapper.getCategoryListVOByPid(0);
        for (CategoryListVO categoryVO : categoryListVO) {
            categoryVO.setChildren(categoryMapper.getCategoryListVOByPid(categoryVO.getId()));
        }

        return categoryListVO;
    }

    @Override
    public List<CategoryL1VO> allCategoryL1VO() {
        List<CategoryL1VO> categoryL1VOList = categoryMapper.selectAllCategoryL1();
        System.out.println(categoryL1VOList);
        return categoryL1VOList;
    }

    @Override
    public Category createCategory(Category category) {
        Date date = new Date();
        category.setAddTime(date);
        category.setUpdateTime(date);
        category.setDeleted(false);
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public int updateCategory(Category category) {
        int update = 0;
        try {
            update = categoryMapper.updateByPrimaryKey(category);
        } catch (Exception e) {
            e.printStackTrace();
            return update;
        }
        return update;
    }

    @Override
    public int deleteCategory(Category category) {
        int delete = categoryMapper.deleteByPrimaryKey(category.getId());
        return delete;
    }

}
